<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/8/11 0011
 * Time: 8:52
 * 邀请业务处理
 */
namespace Api\Controller;
use Think\Controller;
class InvitationController extends Controller {

    function index(){
        $this->display();
//        echo bdUrlAPI(1,"http://www.xxxx.com/BDserverSVN/index.php/Api/V1/getResType/token/9383902f4d6cf51c5af8d7e926c86d47/uid/7");

    }

    /**
     * 邀请
     * @type int 被推荐类型 1普通用户 2跑客
	 * @user_type 推荐人类型 1 用户推荐 2 跑客推荐
     * @uid 推荐人ID
     *  
     **/
    function invitation(){
		$this->assign('user_type',I('user_type'));
        $this->assign('uid',I('uid'));
        $this->assign('type',I('type'));
		if(I('type')=="1"){
			$this->display();
		}else{
			$this->display('invitation_pk');
		}


    }
	
	  /**
     * 邀请pk
     * @type int 被推荐类型 1普通用户 2跑客
	 * @user_type 推荐人类型 1 用户推荐 2 跑客推荐
     * @uid 推荐人ID
     *  
     **/
    function invitation_pk(){
        $this->display();
    }

    /**
     * 邀请校验
     * @tel int 1此手机号已经在平台注册过
     * @type int 类型 1普通用户 2跑客
     * @code int 验证码
     * @user_type 推荐人类型 1 用户推荐 2 跑客推荐
     * @uid 推荐人id
     */
    function invitationcheck(){
        if(I('code') && session('code') == I('code')){
            $type = I('type');
            $user_type=I('user_type');//推荐人类别
            $user_obj=null;
            if(I('user_type')==1){
                $user_obj = M('app_users')->where('user_id='.I('uid'))->field('user_id')->find();
            }elseif(I('user_type')==2){
                $user_obj = M('dispatch')->where('dispatch_id='.I('uid'))->field('dispatch_id')->find();
            }else{
                return RET_NO_V(2,"抱歉，邀请失败");
            }
            if(!$user_obj){
                return RET_NO_V(2,"抱歉，推荐人不存在");
            }

            switch ($type){
                case 1:
                    $app_users = M('app_users');
                    $app_users->startTrans();
                    $ret = $app_users->where(array('mobile'=>I('tel')))->field('mobile')->find();

                    if($ret){
                        echo RET_NO_V(2,"账号已经存在，请下载APP并登陆");
                    }else{
                        //邀请逻辑

                        //新增用户
                        $ret = $app_users->add(array('mobile' => I('tel'), 'user_name' => I('tel'), 'avatar' => 'Uploads/public/icon.png', 'account_status' => 1, 'purse_balance' => 0.00, 'user_level_id' => 1, 'growth_value' => 10, 'validate_code' => I('code'), 'token' => md5(uniqid(rand())), 'regist_time' => date('Y-m-d H:i:s', time()), 'is_shop' => 0));
                        if($ret){
                            //新增关系表
                            $recommend = M('recommend');
                            $recomend_ret = $recommend->add(array(
                                'recom_person_type'=>I('user_type'),        //推荐人类别:1用户，2派单员
                                'recom_person_id'=>I('uid'),                //推荐人id
                                'recommended_person_id'=>$ret,              //被推荐人id
                                'recommended_person_type'=>1,               //被推荐人类型:1用户，2派单员
                                'recommended_time'=>date('Y-m-d H:i:s'),    //推荐时间
                                'recommended_status'=>0                     //推荐状态(0 已推荐未激活 1 已推荐未激活
                            ));
                            //跑客ID
                            $upret = updateRecommendPersonCount(I('uid'),I('user_type'));
                            if($recomend_ret && $upret){
                                $app_users->commit();
                                RET_NO_V(1,"恭喜您,邀请成功");
                            }else{
                                echo RET_NO_V(3,"邀请失败，请稍后再试！");
                                $app_users->rollback();
                            }
                        }else{
                            echo RET_NO_V(3,"邀请失败，请稍后再试！");
                            $app_users->rollback();
                        }
                    }
                    break;
                case 2:
                    $dispatch = M('dispatch');
                    $ret = $dispatch->where(array('mobile'=>I('tel')))->field('mobile')->find();
                    if($ret){
                        echo RET_NO_V(2,"抱歉，此账号已经存在！");
                    }else{
                        //邀请逻辑

                        //新增跑客
                        $dispatch = M('dispatch');
                        $dispatch->startTrans();
                        $ret = $dispatch->add(array('mobile' => I('tel'), 'company_id' => 4, 'avatar' => 'Uploads/public/icon.png', 'is_audit' => 0, 'is_incubator' => 0, 'evaluate_star' => 0, 'growth_value' => 0, 'dispatch_level_id' => 1, 'cash_balance' => 0.00, 'city_id' => 128, 'token' => md5(uniqid(rand()))));
                        if($ret){
                            //新增关系表
                            $recommend = M('recommend');
                            $recomend_ret = $recommend->add(array(
                                'recom_person_type'=>I('user_type'),        //推荐人类别:1用户，2派单员
                                'recom_person_id'=>I('uid'),                //推荐人id
                                'recommended_person_id'=>$ret,              //被推荐人id
                                'recommended_person_type'=>2,               //被推荐人类型:1用户，2派单员
                                'recommended_time'=>date('Y-m-d H:i:s'),    //推荐时间
                                'recommended_status'=>0                     //推荐状态(0 已推荐未激活 1 已推荐未激活
                            ));
                            $upret = updateRecommendPersonCount(I('uid'),I('user_type'));
                            if($recomend_ret && $upret){
                                $dispatch->commit();
                                RET_NO_V(1,"恭喜您,邀请成功");
                            }else{
                                $dispatch->rollback();
                                echo RET_NO_V(3,"邀请失败，请稍后再试！");
                            }
                        }else{
                            $dispatch->rollback();
                            echo RET_NO_V(3,"邀请失败，请稍后再试！");
                        }

                    }

                    break;
            }
        }else{
            echo RET_NO_V(0,"验证码错误！");

        }

    }
	
	function download_pk(){
		$this->display();
	}
	
	function download_pt(){
		$this->display();
	}
	
	function jump_pk(){
		$this->display();
	}
	
	function jump_pt(){
		$this->display();
	}
}